import 'package:get/get.dart';
import 'package:xin_he/utils/xin_he_toast.dart';
import 'package:xin_he/utils/xin_he_user_data.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_event_bus.dart';
import '../../../utils/xin_he_storage.dart';
import 'xin_he_mine_state.dart';

class XinHeMineLogic extends GetxController {
  final XinHeMineState state = XinHeMineState();
  List topTabList = ['云茶籽','茶油卡','消费金红包','余额'];
  RxList topTabValueList = ['0','0','0','0'].obs;
  List orderTabList = ['待付款','待发货','待收货','待评价','售后'];
  List orderTabIcon = ['status_wait_pay','status_wait_deliver','status_wait_accept','status_wait_comment','status_after_sale',];

  // List funcTitleList = [
  //   '实名认证','拼团订单','我的团队','优惠券',
  //   '我的收藏','商家入驻 ','商家管理','邀请好友',
  //   '工作室','银行卡','地址管理','联系客服',
  //  ];
  List funcTitleList = [
    '实名认证','我的团队','优惠券',
    '我的收藏','邀请好友',
    '工作室','银行卡','地址管理','联系客服',
  ];
  // List funcTitleIcon = [
  //   'func_real_auth','func_group_book_orders','func_team','func_coupon',
  //   'func_favor','func_seller_enter','func_seller_manager','func_contact',
  //   'func_work_place','func_bank','func_address','func_help',
  // ];
  List funcTitleIcon = [
    'func_real_auth','func_team','func_coupon',
    'func_favor','func_contact',
    'func_work_place','func_bank','func_address','func_help',
  ];


  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    //用户资料变更成功事件通知
    bus.on(XinHeStorage.KdUserInfoUpdateSuc, (arg) async {
      //登录成功后更新用户数据,刷新页面
      update();
    });
    //收到退出登录成功通知
    bus.on(XinHeStorage.KdUserLogoutNoti, (arg) {
      //退出成功后重置用户数据和app状态，刷新页面
      update();
    });


    bus.on(XinHeStorage.KNeedRefresh, (arg) async {

      if(arg == 4) {
        await   requestMyProperty();
      }

    });

  }

  @override
  void onReady() {
    // TODO: implement onInit
    super.onReady();

    requestMyProperty();

  }



  requestMyProperty() async{

    String userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic>map = {
      'userId':userId
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/member/findByMemberAssetCount',params: map,isPost: false);

    if(!result.result){

    }

    Map json =result.data['data'];

    List<String> items = [];
    items.add('${json['yunChaSeed']}');
    items.add('${json['teaOilCard']}');
    items.add('${json['consumerGoldRedEnvelope']}');
    items.add('${json['rmb']}');
    topTabValueList.value = items;


  }

  requestMyProfile() async{

    String userId = await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {

      'userId':userId

    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/member/findByMember',params: map,isPost: false);

    if(!result.result){

    }



  }


  loginOut() async {
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic>map = {

      'userId':userId
    };
    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/member/appLoginOut',params: map,isPost: false);

    if(!result.result){

      return;
    }

    if(result.data['code'] == 0){
      //发出退出登录成功通知
      bus.emit(XinHeStorage.KdUserLogoutNoti);
      //成功
      XinHeToast.center('您已退出登录');
    }else {
      XinHeToast.center(result.data['message']);

    }




  }








}
